
using System;
using System.IO;
using System.Text;

namespace QQZiFramework.Mvc
{
    public static class ConvertHelper
    {
        private static string utf8_gb2312;
        private static string utf8_big5;
        static System.Collections.SortedList sortedList = new System.Collections.SortedList();
        static System.Collections.SortedList sortedList2 = new System.Collections.SortedList();

        public static void init(string rootPath)
        {
            string path = System.IO.Path.Combine(rootPath, "Traditional.txt");
            if (System.IO.File.Exists(path))
            {
                try
                {
                    System.IO.FileStream fs = new FileStream(path, FileMode.Open);
                    StreamReader sr = new StreamReader(fs);
                    utf8_gb2312 = sr.ReadLine();
                    utf8_big5 = sr.ReadLine();
                    sr.Close();
                    fs.Close();

                    for (int i = 0; i < utf8_gb2312.Length; i++)
                    {
                        sortedList.Add(utf8_gb2312[i], utf8_big5[i]);
                        if (!sortedList2.ContainsKey(utf8_big5[i]))
                            sortedList2.Add(utf8_big5[i], utf8_gb2312[i]);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return;
                }
            }

        }

        public static string ToTraditional(string str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                object bit5 = sortedList[c];
                if (bit5 == null) sb.Append(c);
                else sb.Append(bit5);
            }
            return sb.ToString();
        }

        public static string ToSimplified(string str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                object gb = sortedList2[c];
                if (gb == null) sb.Append(c);
                else sb.Append(gb);
            }
            return sb.ToString();
        }

    }



}